<?php
class RM_PickupDropoff extends RM_Model
{
    protected $_name = 'rm_pickupdropoff';

    /**
     * Returns Location Data by Location ID
     *
     * @return Zend_Db_Table_Rowset
     */
    function getLocationData($id){
        return $this->fetchAll("id='".$id."'");
    }

    function getAll(){
        return $this->fetchAll();
    }

    /**
     * returns all locations by the Unit ID
     *
     * @param string $unitID
     * €return array
     */
    function getByUnitID($unitID){
        $locations = $this->getAll()->toArray();

        $units = array();
        foreach($locations as $row){

            $unitsString = $row['units'];
            $unitsArray = explode(",",$unitsString); // convert everything to array
            $unitsArray = array_unique($unitsArray); // remove duplicates
            $unitsArray = array_filter($unitsArray,"is_numeric"); // filter only on numeric values

            if ($row['units'] === '0'){
                // all units
                $units[] = array("id"=>$row['id'],"units"=>$row['units'],"name"=>$row['name'],"address1"=>$row['address1'],"address2"=>$row['address2'],"address3"=>$row['address3'],"city"=>$row['city'],"state"=>$row['state'],"postcode"=>$row['postcode'],"country"=>$row['country']);
            } else {
                if (in_array($unitID,$unitsArray)){
                    // if we find the unit in the array add it to the list of locations...
                    $units[] = array("id"=>$row['id'],"units"=>$row['units'],"name"=>$row['name'],"address1"=>$row['address1'],"address2"=>$row['address2'],"address3"=>$row['address3'],"city"=>$row['city'],"state"=>$row['state'],"postcode"=>$row['postcode'],"country"=>$row['country']);
                }
            }
        }

        return $units;
    }
}